
package com.hd.sup.modules.terminal.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.common.constant.BizConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.GenUtil;
import com.hd.sup.modules.terminal.model.Terminal;
import com.hd.sup.modules.terminal.service.ITerminalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title TerminalController.java
 * @Description 终端控制器
 * @Package com.hd.sup.modules.terminal.controller
 * @date 2020/12/31 4:47 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/terminal")
public class TerminalController extends BaseController {


    @Resource
    private ITerminalService terminalService;

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 企业列表查询
     * @date 2020/12/31 2:57 下午
     * @params [requestData]
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody RequestData requestData) {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String terminalNo = requestData.getString("terminalNo");
        String terminalType = requestData.getString("terminalType");
        String installDateStr = requestData.getString("installDateStr");
        //入参校验
        if (current == null || size == null) return Result.error("参数 current 或 size 不允许为空");

        Terminal param = new Terminal(terminalNo, terminalType, installDateStr);

        Page<Map<String, Object>> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        List<Map<String, Object>> result = terminalService.pageList(page, param);
        page.setRecords(result);
        return Result.success(page);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 删除企业信息
     * @date 2020/12/31 3:25 下午
     * @params [ids]
     */
    @DeleteMapping("/delete/{ids}")
    public Result<?> delete(@PathVariable() String ids) {
        if (StringUtils.isBlank(ids)) return Result.error("参数 ids 为空");
        terminalService.removeByIds(Arrays.asList(ids.split(BizConstant.Common.SEPARATOR.key())));
        return Result.success();
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 保存或更新
     * @date 2020/12/31 3:37 下午
     * @params [ids]
     */
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(Terminal terminal) {
        //入参校验
        if (terminal == null) return Result.error("参数为空");

        if (StringUtils.isBlank(terminal.getId())) {
            terminal.setId(GenUtil.newId());
            terminal.setCreateBy(getCurrentUser().getUsername());
        }
        terminal.setUpdateBy(getCurrentUser().getUsername());
        terminal.insertOrUpdate();
        return Result.success(terminal);
    }


}
